package com.learn.micro_mall.repository.category;

import com.learn.micro_mall.dao.category.CategoryMapper;
import com.learn.micro_mall.dao.category.CategoryParamMapper;
import com.learn.micro_mall.dao.category.model.Category;
import com.learn.micro_mall.dao.category.model.CategoryParam;
import com.learn.micro_mall.domain.entity.category.CategoryEntity;
import com.learn.micro_mall.domain.vo.category.CategoryId;
import com.learn.micro_mall.repository.convertor.category.CategoryConvert;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Repository
public class CategoryRepository {

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private CategoryParamMapper categoryParamMapper;
    /**
     * 根据商品分类Id获取分类
     * @param categoryId
     * @return
     */
    public CategoryEntity getByCategoryId(CategoryId categoryId){
        Category category = categoryMapper.getByCategoryId(categoryId.getCategoryId());
        return CategoryConvert.INSTANCES.toCategory(category);
    }


    /**
     * 根据商品分类Id获取分类
     * @param categoryEntity
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean insertCategory(CategoryEntity categoryEntity){
        Category category = new Category();
        category.setCategoryId(categoryEntity.getCategoryId().getCategoryId());
        category.setCategoryName(categoryEntity.getCategoryName());
        category.setParentCategoryId(categoryEntity.getParentCategoryId());
        categoryMapper.insertSelective(category);
        if(categoryEntity.getCategoryParams() != null && !categoryEntity.getCategoryParams().isEmpty()) {
            for (String categoryParamKey : categoryEntity.getCategoryParams()) {
                CategoryParam categoryParam = new CategoryParam();
                categoryParam.setCategoryId(categoryEntity.getCategoryId().getCategoryId());
                categoryParam.setCategoryParam(categoryParamKey);
                categoryParamMapper.insert(categoryParam);
            }
        }
        return true;
    }

    /**
     * 更新商品分类信息
     * @param categoryEntity
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateCategory(CategoryEntity categoryEntity) {
        Category category = new Category();
        category.setCategoryId(categoryEntity.getCategoryId().getCategoryId());
        category.setCategoryName(categoryEntity.getCategoryName());
        categoryMapper.updateCategoryBaseMessage(category);
        categoryParamMapper.deleteCategoryParam(categoryEntity.getCategoryId().getCategoryId());
        if(categoryEntity.getCategoryParams() != null && !categoryEntity.getCategoryParams().isEmpty()) {
            for (String categoryParamKey : categoryEntity.getCategoryParams()) {
                CategoryParam categoryParam = new CategoryParam();
                categoryParam.setCategoryId(categoryEntity.getCategoryId().getCategoryId());
                categoryParam.setCategoryParam(categoryParamKey);
                categoryParamMapper.insert(categoryParam);
            }
        }
    }

    /**
     * 更新商品分类信息
     * @param categoryId
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteCategory(CategoryId categoryId) {
        categoryMapper.deleteByCategoryId(categoryId.getCategoryId());
        categoryParamMapper.deleteCategoryParam(categoryId.getCategoryId());
    }
}
